package controller;

import com.jfinal.core.Controller;
import constant.Msg;
import model.Recipe;
import validate.RecipeValidate;

public class RecipeController extends Controller {
	public void index() {
		String id = getPara("id");
		String type = getPara("type");
		String title = getPara("title");
		String technology = getPara("technology");
		String rate = getPara("rate");
		String foods = getPara("foods");
		String region_taste = getPara("region_taste");
		String order = getPara("order");
		String offset = getPara("offset");
		String length = getPara("length");
		
		String valid = null;
		switch (getRequest().getMethod()) {
		case "GET":
			valid = RecipeValidate.get(type, title, offset, length);
			if (valid != null) {
				renderJson(valid);
				return;
			}
			renderJson(Recipe.dao.select(type, title, technology, rate, foods, region_taste, offset, length, order));
			break;
		case "DELETE":
			valid = RecipeValidate.delete(id);
			if (valid != null) {
				renderJson(valid);
				return;
			}
			renderJson(Recipe.dao.delete(Integer.parseInt(id)));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}

	/**
	 * 暂时不用
	 */
	public void random() {
		String length = getPara("length");
		String valid = null;
		switch (getRequest().getMethod()) {
		case "GET":
			valid = RecipeValidate.random(length);
			if (valid != null) {
				renderJson(valid);
				return;
			}
			renderJson(Recipe.dao.random(Integer.parseInt(length)));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}
	
	public void getFilters() {
		String filter = getPara("filter");
		switch (getRequest().getMethod()) {
		case "GET":
			String valid = RecipeValidate.getFilters(filter);
			if (valid != null) {
				renderJson(valid);
				return;
			}
			renderJson(Recipe.dao.getFilter(filter));break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}

	public void detail() {
		String id = getPara("id");
		switch (getRequest().getMethod()) {
		case "GET":
			String valid = RecipeValidate.delete(id);
			if (valid != null) {
				renderJson(valid);
				return;
			}
			renderJson(Recipe.dao.detail(Integer.parseInt(id)));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}
	
	public void shicai() {
		switch (getRequest().getMethod()) {
		case "GET":
			String keyword = getPara("keyword");
			if(keyword==null){
				renderJson(Msg.ERROR_PARA_NUM);
				return;
			}
			renderJson(Recipe.dao.shicai(keyword));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}
	
	//智能组菜
	public void cook() {
		switch (getRequest().getMethod()) {
		case "GET":
			String foodsname = getPara("foodsname");
			if(foodsname==null){
				renderJson(Msg.ERROR_PARA_NUM);
				return;
			}
			renderJson(Recipe.dao.cook(foodsname));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
		}
	}
}
